<?php

namespace App\Console\Commands;

use App\Models\Archive;
use App\Models\Category;
use Conner\Tagging\Model\Tag;
use Illuminate\Console\Command;

class CalculateHotTagsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'zhitian:hot-tags';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计热门标签';

    public $cache_key_prefix = 'category_hot_tags_';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $tags = Tag::pluck('name');
        $categories = Category::all();
        $categories->map(function ($category) use ($tags) {
            $counts = [];
            foreach ($tags as $tag) {
                $count = Archive::where('category_id', $category->id)->withAnyTag($tag)->count();
                if ($count > 0) {
                    $counts[] = [
                        'tag' => $tag,
                        'count' => $count
                    ];
                }
            }
            return [
                'category' => $category->id,
                'counts' => $counts
            ];
        });
    }

    private function getCategoryHotTags($category)
    {

    }
    private function calculateCategoryHotTags($category)
    {
        $tags = Tag::pluck('name');

    }
}
